let data = {
    "id": "", "state": "", "createtime": "", "deadline": "", "title": "","questionList": []
}
let Data_list={
    "list":[]
}

const create = document.querySelector('.create');
const manage = document.querySelector('.manage');
const edit = document.querySelector('.edit');
const option1 = document.querySelector('.option1');
const option2 = document.querySelector('.option2');
const option3 = document.querySelector('.option3');
const question = document.querySelector(".edit .question");
const add1 = document.querySelector('.add1')
const add2 = document.querySelector('.add2')

create.addEventListener('click', action)
option1.addEventListener('click',createOption1)
option2.addEventListener('click',createOption2)
option3.addEventListener('click',createOption3)
add2.addEventListener('click',()=>{
    add1.style.display='flex';
})
// 从新建页面转编辑问卷
function action() {
    window.location.href = "html/edit.html";
    // create.style.display = 'none';
    // manage.style.display = 'block';
    // edit.style.display = 'block';
}

function createEdit(){
    if(localStorage.getItem('Id')==null){
        localStorage.setItem('Id','1')
    }
    else{
        ListId = JSON.parse(localStorage.getItem('Id'))
        if(ListId>20)
            ListId=0
        localStorage.setItem('Id',ListId+1)
        data.id=ListId
    }
    data.createtime=new Date();
}

function saveUpdata(){
    data.title = document.getElementById("title").value;
    data.deadline = document.getElementById("deadline").value;
}

function saveQuestionList(){
    this.chessid=1
    for(i=1;i<num;i++){
        if(data.questionList[i-1].type=="radio"){
            this.chessid=i
            data.questionList[i-1].name=document.getElementById(''+this.chessid+'0').value
            data.questionList[i-1].answer[0].text= document.getElementById(''+this.chessid+'1').value
            data.questionList[i-1].answer[1].text = document.getElementById(''+this.chessid+'2').value
        }
        else if (data.questionList[i - 1].type =="checkbox"){
            this.chessid = i
            data.questionList[i - 1].name = document.getElementById('' + this.chessid + '0').value
            data.questionList[i - 1].answer[0].text = document.getElementById('' + this.chessid + '1').value
            data.questionList[i - 1].answer[1].text = document.getElementById('' + this.chessid + '2').value
            data.questionList[i - 1].answer[2].text = document.getElementById('' + this.chessid + '3').value
            data.questionList[i - 1].answer[3].text = document.getElementById('' + this.chessid + '4').value
        }
        else if (data.questionList[i - 1].type =="text"){
            this.chessid = i
            data.questionList[i - 1].name = document.getElementById('' + this.chessid + '0').value
            data.questionList[i-1].answer[0].text = document.getElementById(''+this.chessid+'1').value
            data.questionList[i - 1].optional = document.getElementById('' + this.chessid + '2').checked
        }
    }
}



let num=1;
//创建单选题
function createOption1(){
    let c1 = document.createElement('div');
    // c1.style.height="100px";
    c1.innerHTML = "&nbsp;&nbsp;Q"+num
        + "<input id='" + num +"0' style='margin-left:10px;' type='text' value='单选题'><br>"
        + "<input style='margin-left:40px;' type='radio' value='选项一'><input type='text' value='选项一' id='" + num +"1'><br>"
        + "<input style='margin-left:40px;' type='radio' value='选项二'><input type='text' value='选项二' id='" + num +"2'>"
    question.appendChild(c1);
    data.questionList.push(
        {
            "name":"",
            "type": "radio", //单选题
            "answer": [
                { //answer[0]
                    "text": "",
                    "total": 0,
                },
                { //answer[1]
                    "text": "",
                    "total": 0,
                }
            ]
        });
    num++;
    add1.style.display = 'none';
}

//创建多选题
function createOption2() {
    let c1 = document.createElement('div');
    // c1.style.height = "100px";
    c1.innerHTML = "&nbsp;&nbsp;Q" + num
        + "<input id='" + num +"0' style='margin-left:10px;' type='text' value='多选题'><br>"
        + "<input style='margin-left: 40px;' type='checkbox' value='选项一'><input type='text' value='选项一' id='"+num+"1'><br>"
        + "<input style='margin-left: 40px;' type='checkbox' value='选项二'><input type='text' value='选项二' id='" + num +"2'><br>"
        + "<input style='margin-left: 40px;' type='checkbox' value='选项三'><input type='text' value='选项三' id='" + num +"3'><br>"
        + "<input style='margin-left: 40px;' type='checkbox' value='选项四'><input type='text' value='选项四' id='" + num +"4'><br>"
    question.appendChild(c1);
    data.questionList.push(
        {
            "name": "",
            "type": "checkbox", //多选题
            "answer": [
                { "text": "", "total": 0 }, //answer[0]
                { "text": "", "total": 0 }, //answer[1]
                { "text": "", "total": 0 }, //answer[2]
                { "text": "", "total": 0 }, //answer[3]
            ]
        });
    num++;
    add1.style.display = 'none';
}

//创建文本题
function createOption3() {
    let c1 = document.createElement('div');
    c1.innerHTML = "&nbsp;&nbsp;Q" + num
        + "<input id='"+num+"0' style='margin-left:10px;' type='text' value='文本题'>"
        + "<div style='float: right;'><input id='" + num +"2' value='1' checked='checked' style='display:inline-block;margin-top:20px ;' type='checkbox'>"
        + "<p style='font-size:13px;display:inline-block;margin-left:10px;'>此题是否必填</p></div><br>"
        + "<textarea id='"+num+"1' style='margin: 20px 0 0 40px; ' cols='50' rows='5'></textarea>"
    question.appendChild(c1);
    data.questionList.push(
        {
            "name": "",
            "type": "text", //文本题
            "answer": [
                { "text": "" } //answer[0]
            ],
            "optional": ""//true必填  false选填
        });
    num++;
    add1.style.display = 'none';
}


//保存和发布
function remainFirst(){
    saveUpdata()
    saveQuestionList()
    data.state='未发布'
    localStorage.setItem('data_list',JSON.stringify(data))
    if (JSON.parse(localStorage.getItem("Data_list"))!=null){
    Data_list = JSON.parse(localStorage.getItem("Data_list"))
    }
    if (Data_list.list.length - 1 > 0)
        if (Data_list.list[Data_list.list.length - 1].updata.title == data.title)
            Data_list.list.pop()
    Data_list.list.push({
        "Id":data.id,
        "updata": data
    })
    localStorage.setItem('Data_list',JSON.stringify(Data_list))
    // window.location.href="./01-1.html";
    window.location.href = "./1-1.html";
}

function remainSecond(){
    saveUpdata()
    saveQuestionList()
    data.state='发布中'
    localStorage.setItem('data_list', JSON.stringify(data))
    if (JSON.parse(localStorage.getItem("Data_list")) != null) {
        Data_list = JSON.parse(localStorage.getItem("Data_list"))
    }
    if (Data_list.list.length - 1 > 0)
        if (Data_list.list[Data_list.list.length - 1].updata.title == data.title)
            Data_list.list.pop()
    Data_list.list.push({
        "Id": data.id,
        "updata": data
    })
    localStorage.setItem('Data_list', JSON.stringify(Data_list))
    window.location.href = "./1-1.html";
}

document.querySelector(".submit").addEventListener("click",Display4)
function Display4(){
    suredead = document.getElementById('deadline').value
    document.getElementById('suredeadline').textContent ="(此问卷截至日期为"+suredead+")"
    document.querySelector(".Sure").style.setProperty('display','flex')

}
document.querySelector(".no").addEventListener("click",Display5)
function Display5(){
    document.querySelector(".Sure").style.setProperty('display','none');
}

// function initEdit(n){
//     if(n==0){
//          window.location.href = "./edit.html?id=" + 0 + "";
//     }
//     else{

//     }
//     window.location.href = "./edit.html?id=" + 0 + "";
// }
// const Test = document.querySelector('.Test')
// Test.addEventListener('click', createEdit())